package com.hjj.srb.core.web.admin;

import com.hjj.common.result.R;
import com.hjj.srb.core.pojo.entity.IntegralGrade;
import com.hjj.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
@Slf4j
@Api(tags = "积分等级管理")
@CrossOrigin //跨域
@RestController
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {

	@Resource
	private IntegralGradeService integralGradeService;

	@ApiOperation("积分等级列表")
	@GetMapping("/list")
	public R listAll(){

		//日志文件测试
		log.info("hi i'm helen");
		log.warn("warning!!!");
		log.error("it's a error");

		List<IntegralGrade> list = integralGradeService.list();
		return R.ok().data("list", list).msg("获取列表成功");
	}

	@ApiOperation(value = "根据id删除数据记录", notes = "逻辑删除")
	@DeleteMapping("/remove/{id}")
	public R removeById(@PathVariable("id") Long id){
		boolean result = integralGradeService.removeById(id);
		if (result){
			return R.ok().msg("删除成功");
		} else {
			return R.error().msg("删除失败");
		}
	}

	@ApiOperation("新增积分等级")
	@PostMapping("/save")
	public R save(
			@ApiParam(value = "积分等级对象", required = true)
			@RequestBody IntegralGrade integralGrade){

		//测试:抛运行时异常
//		if (integralGrade.getBorrowAmount() == null)
//			throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
		//优化:使用断言代替if结构
//		Assert.notNull(integralGrade.getBorrowAmount(), ResponseEnum.BORROW_AMOUNT_NULL_ERROR);

		boolean result = integralGradeService.save(integralGrade);
		if (result){
			return R.ok().msg("保存成功");
		} else {
			return R.error().msg("保存失败");
		}
	}
	
	@ApiOperation("根据id查询积分等级")
	@GetMapping("/list/{id}")
	public R getById(
			@ApiParam(value = "数据id", required = true, example = "1")
			@PathVariable Long id){

		IntegralGrade integralGrade = integralGradeService.getById(id);
		if (integralGrade != null){
			return R.ok().msg("查询成功").data("record",integralGrade);
		} else {
			return R.error().msg("无此id的记录");
		}
	}

	@ApiOperation("更新积分等级")
	@PutMapping("/update")
	public R updateById(
			@ApiParam(value = "数据id", required = true, example = "1")
			@RequestBody IntegralGrade integralGrade){

		boolean result = integralGradeService.updateById(integralGrade);
		if (result){
			return R.ok().msg("更新成功");
		} else {
			return R.error().msg("更新失败");
		}
	}

	
}
